/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 */
package seedpod.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import seedpod.model.SeedpodDO;

public class Util {

	public static void dispatch( String url, ServletRequest request, ServletResponse response ) throws ServletException, IOException {
		if ( response instanceof HttpServletResponse ) url = ((HttpServletResponse)response).encodeURL( url );
		RequestDispatcher dispatcher = request.getRequestDispatcher( url );
		dispatcher.forward( request, response );
	}

	public static String encodeHTML( String s ) {
		if ( s == null ) return null;
		StringBuffer out = new StringBuffer();
		for ( int i = 0; i < s.length(); i++ ) {
			char c = s.charAt( i );
			if ( c > 127 || c == '"' || c == '<' || c == '>' ) out.append( "&#" + (int)c + ";" );
			else out.append( c );
		}
		return out.toString();
	}

	/**
	 * attempts to build a absolute url from a relative path
	 * 
	 * @param webapp
	 *            String denoting the docBase that the servlet is installed
	 * @param path
	 *            String path of the resource
	 * @return absolute url
	 */
	public static String getAbsoluteURL( String webapp, String path ) {
		if ( path.charAt( 0 ) == '/' ) {
			if ( webapp.charAt( webapp.length() - 1 ) == '/' ) return webapp + path.substring( 1, path.length() );
			else return webapp + path;
		} else return webapp + "/" + path;
	}

	public static Timestamp getCurrentTimestamp() {
		return new Timestamp( System.currentTimeMillis() );
	}

	public static String getSeedpodInternalId( SeedpodDO obj ) {
		return getSeedpodInternalId( obj.getMetaCls().getName(), obj.getOid() );
	}

	public static String getSeedpodInternalId( String clsType, Integer oid ) {
		return clsType + "[" + oid + "]";
	}

	public static void include( String url, ServletRequest request, ServletResponse response ) throws ServletException, IOException {
		if ( response instanceof HttpServletResponse ) url = ((HttpServletResponse)response).encodeRedirectURL( url );
		RequestDispatcher dispatcher = request.getRequestDispatcher( url );
		dispatcher.include( request, response );
	}

	// This will redirect the whole page using javascript instead of a div if ajax is used.
	public static void jsRedirect( String loginUrl, ServletRequest request, ServletResponse response ) {
		PrintWriter out;
		try {
			out = response.getWriter();
			out.println( "<script>window.location='../signin.jsp';</script>" );
			out.flush();
		} catch (IOException e) {
			/**
			 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of
			 * the license included
			 * 
			 * @author Maya Hao Li
			 */
		}
	}

	public static void redirect( String url, ServletRequest request, ServletResponse response ) throws ServletException, IOException {
		if ( response instanceof HttpServletResponse ) {
			HttpServletResponse hResponse = (HttpServletResponse)response;
			url = hResponse.encodeURL( url );
			hResponse.sendRedirect( url );
		}
	}

}
